<template>
  <div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                tooltip: '编辑',
                onClick: handleAdd.bind(null, 2, record),
              },
              {
                label: '删除',
                tooltip: '删除',
                onClick: handleDel.bind(null, record),
              },
            ]"
        /></template>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleAdd(1, null)">新建</a-button>
      </template>
      <template #status="{ column, record }">
        <Badge
          v-if="column.dataIndex === 'status'"
          :status="statusMap[record.status].status"
          :text="statusMap[record.status].text"
        />
      </template>
    </BasicTable>
    <AddedModal @register="register" @reload="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import AddedModal from './modal/ added.vue';
  import { Badge, Modal } from 'ant-design-vue';
  import { DictCategoryList, DictCategoryRemove } from '@/api/platform/dictionary';
  import { getBasicColumns } from './tableData';
  import { ref } from 'vue';
  import { useModal } from '@/components/Modal';
  import { IDictCategoryListResult } from '@/api/platform/dictionary/type';
  import { useMessage } from '@/hooks/web/useMessage';

  const { createMessage } = useMessage();

  const statusMap = ref({
    0: {
      status: 'success',
      text: '正常',
    },
    1: {
      status: 'default',
      text: '禁用',
    },
  });

  const [registerTable, { reload }] = useTable({
    title: '',
    api: DictCategoryList,
    columns: getBasicColumns(),
    rowKey: 'id',
    useSearchForm: false,
    showTableSetting: true,
    tableSetting: { fullScreen: true },
    showIndexColumn: true,
    clickToRowSelect: false,
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action',
    },
  });

  const [register, { openModal }] = useModal();

  function handleAdd(type: number, row: any) {
    openModal(true, {
      type,
      row,
    });
  }

  function handleDel(row: IDictCategoryListResult) {
    Modal.confirm({
      title: '提示',
      content: '该操作将删除该分类',
      onOk: async () => {
        await DictCategoryRemove(row.id);
        createMessage.success('删除成功！');
        reload();
      },
    });
  }
</script>
